'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
exports.main = async (event, context) => {
	const {
		userId,
		authorId
	} = event;

	const user = await db.collection('user').doc(userId).get();
	const authorLikeIds = user.data[0].author_likes_ids;
	let returnMsg = '';
	let author_ids = null;

	//判断是否关注
	if (authorLikeIds.includes(authorId)) {
		//删除
		author_ids = dbCmd.pull(authorId);
		returnMsg = '取消关注成功'
	} else {
		//增加
		author_ids = dbCmd.addToSet(authorId);
		returnMsg = '关注成功'
	}
	//更新数据库
	await db.collection('user').doc(userId).update({
		author_likes_ids: author_ids
	});

	return {
		code: 0,
		data: {
			returnMsg
		}
	}
};